<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Unite extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('unite_model');
    }
    
    function index() {
        $unites = $this->unite_model->getList();
        
        $data = array(
            'unites' => $unites
        );
        
        $this->layout
                ->title('Mes unités')
                ->js('functions')
                ->js('controllers/unite')
                ->view('unite/index', $data);
    }
    
    public function table() {
        $unites = $this->unite_model->getList();
        return json_encode($unites);
    }
    
    /***************************************************************************
     * EDIT
     **************************************************************************/
    // Create
    public function create() {
        $this->load->view('unite/edit');
    }
    
    public function createDB() {
        $UNITE_LIBELLE = $this->input->get('UNITE_LIBELLE');
        $UNITE_CODE = $this->input->get('UNITE_CODE');
        $this->unite_model->create($UNITE_LIBELLE, $UNITE_CODE);
        echo $this->table();
    }
    
    // Update
    public function update() {
        $UNITE_ID = $this->input->get('UNITE_ID');
        $unite = $this->unite_model->get($UNITE_ID);
        
        $data = array(
            'unite' => $unite
        );
        
        $this->load->view('unite/edit', $data);
    }
    
    public function updateDB() {
        $UNITE_ID = $this->input->get('UNITE_ID');
        $UNITE_LIBELLE = $this->input->get('UNITE_LIBELLE');
        $UNITE_CODE = $this->input->get('UNITE_CODE');
        $this->unite_model->update($UNITE_ID, $UNITE_LIBELLE, $UNITE_CODE);
        echo $this->table();
    }
    
    // Delete
    public function delete() {
        $UNITE_ID = $this->input->get('UNITE_ID');
        $this->unite_model->delete($UNITE_ID);
        echo $this->table();
    }
}